package com.evidencer.java.dao;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.evidencer.java.domain.Student;
import com.evidencer.java.exception.StudentNotFoundException;
import com.evidencer.java.utils.XmlUtils;

/**
 * @Title: StudentDao.java
 * @Package com.evidencer.java.dao
 * @Description: TODO
 * @author lyz lyzpoq_gmail_com
 * @date 2014/01/13 13:30:33
 * @version V1.0
 */
public class StudentDao {

	/**
	 * 追加学生考试信息
	 * 
	 * @Title: add
	 * @Description: TODO
	 * @param @param s
	 * @return void
	 * @throws
	 */
	public void add(Student s) {
		// <student examid="1345667" studentid="1235">
		// <name>张三</name>
		// <location>南京</location>
		// <grade>80</grade>
		// </student>
		try {
			Document document = XmlUtils.getDocument();
			// 创建新节点
			Element student_tag = document.createElement("student");
			Element name_tag = document.createElement("name");
			Element location_tag = document.createElement("location");
			Element grade_tag = document.createElement("grade");

			// 设置节点属性
			student_tag.setAttribute("examid", s.getExamid());
			student_tag.setAttribute("studentid", s.getStudentid());
			name_tag.setTextContent(s.getName());
			location_tag.setTextContent(s.getLocation());
			grade_tag.setTextContent(s.getGrade()+"");

			// 获得根节点
			Element root_tag = (Element) document.getElementsByTagName("exam")
					.item(0);

			// 追加节点
			student_tag.appendChild(name_tag);
			student_tag.appendChild(location_tag);
			student_tag.appendChild(grade_tag);

			root_tag.appendChild(student_tag);

			// 更新节点
			XmlUtils.updateDocument(document);

		} catch (Exception e) {
			throw new RuntimeException();
		}

	}

	/**
	 * 查找学生考试信息
	 * 
	 * @Title: find
	 * @Description: TODO
	 * @param @param studentid
	 * @param @return
	 * @return Student
	 * @throws
	 */
	public Student find(String studentid) {
		Student s = new Student();
		try {
			Document document = XmlUtils.getDocument();
			NodeList studentNodes = document.getElementsByTagName("student");
			for (int i = 0; i < studentNodes.getLength(); i++) {
				Element e = (Element) studentNodes.item(i);
				System.out.println("--"+e.getAttribute("studentid")+"--");
				if (studentid.equals(e.getAttribute("studentid"))) {
					s.setExamid(e.getAttribute("examid"));
					s.setStudentid(e.getAttribute("studentid"));
					s.setName(e.getElementsByTagName("name").item(0)
							.getTextContent());
					s.setLocation(e.getElementsByTagName("location").item(0)
							.getTextContent());
					s.setGrade(Double.parseDouble(e.getElementsByTagName("grade").item(0)
							.getTextContent()));
					return s;
				}
			}
			throw new StudentNotFoundException(studentid + "的相关信息不存在");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * @throws StudentNotFoundException 
	 * 删除学生考试信息
	 * 
	 * @Title: delete
	 * @Description: TODO
	 * @param @param studentid
	 * @return void
	 * @throws
	 */
	public void delete(String studentid) throws StudentNotFoundException {
		try {
			Document document = XmlUtils.getDocument();
			NodeList studentNodes = document.getElementsByTagName("student");
			for (int i = 0; i < studentNodes.getLength(); i++) {
				Element e = (Element) studentNodes.item(i);
				if (studentid.equals(e.getAttribute("studentid"))) {
					e.getParentNode().removeChild(e);
					// 更新节点
					XmlUtils.updateDocument(document);
					return;
				}
			}
			throw new StudentNotFoundException();
		}catch (StudentNotFoundException e) {
			throw new StudentNotFoundException(studentid + "的相关信息不存在");
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
